package com.dkha.server.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dkha.common.enums.ApiUrlEnum;
import com.dkha.common.http.HttpUtil;
import com.dkha.common.modules.vo.ApiVO;
import com.dkha.common.modules.vo.face.ApiAlarmVO;
import com.dkha.common.modules.vo.face.PageVO;
import com.dkha.common.modules.vo.warning.WarningBayonetReturnVO;
import com.dkha.common.result.CommonResult;
import com.dkha.common.util.UtilValidate;
import com.dkha.server.common.config.InitStartDateConfig;
import com.dkha.server.mappers.FaceInformationMapper;
import com.dkha.server.modules.entities.*;
import com.dkha.server.services.IControlTaskService;
import com.dkha.server.services.IFaceLibraryService;
import com.dkha.server.services.IPortraitService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Description: TODO(please write your description)
 * All rights 成都电科慧安
 * @Title: IndexContral
 * @Package com.dkha.server.controllers
 * @author: panhui
 * @date: 2019/12/13 16:53
 * @Copyright: 成都电科慧安
 */
@RestController
@RequestMapping(value = "/index")
@Api(tags = "首页数据接口")
@Slf4j
public class IndexContral extends  CommonResult{

    @Autowired
    private HttpUtil httpUtil;

    @Value("${api.server.prefix}")
    private String link;

    @Autowired
    private Gson gson;


    @Autowired
    private IFaceLibraryService iFaceLibraryService;
    @Autowired
    private IControlTaskService iControlTaskService;

    @Autowired
    private IPortraitService iPortraitService;

    @Autowired
    private FaceInformationMapper faceInformationMapper;


    @GetMapping("/show")
    @ApiOperation(value = "首页展示")
    public CommonResult todayBayonet()
    {

        Date nowdate=new Date();
        //正常运行天数
        Long a=nowdate.getTime();
        Long b=InitStartDateConfig.initDate.getTime();
//        System.out.println("a-b的时间"+ (a-b));
        Integer daysOfOperation=(int)((nowdate.getTime()-InitStartDateConfig.initDate.getTime())/(3600000*24)+1);//单位为毫秒
        daysOfOperation += 259;
        //人像库数量
        Integer libNumber=0;
        //人脸数量
        Integer faceNumber=0;
        //布控任务数
        Integer taskNumber=0;
        //预警数量
        Long warnNumber=0L;
        try
        {
            //人像库数量
            QueryWrapper<FaceLibrary> queryWrapper=new QueryWrapper<>();
            FaceLibrary faceLibrary=new FaceLibrary();
            faceLibrary.setIsValid("Y");
//            faceLibrary.setFactoryType("-1");
            queryWrapper.setEntity(faceLibrary);
            queryWrapper.ne("factory_type","-1");
            System.out.println("人像库sql：");
            libNumber=iFaceLibraryService.getBaseMapper().selectCount(queryWrapper);
        }catch (Exception e){}

        try
        {
            Long numbershiwan =1681443666572L;
            //要求为 1万*10+9格式的数字。。。。。。以下代码对功能没有实际作用
            //先获取各人像库的idfactor
            QueryWrapper<FaceLibrary> queryWrapper1 =new QueryWrapper<>();
            List<FaceLibrary> id = iFaceLibraryService.getBaseMapper().selectList(queryWrapper1);
            Long face_id[] =new Long[libNumber];
            for (int i = 0; i < face_id.length; i++) {
                face_id[i] = id.get(i).getIdFactory();
            }
            //在根据每个人像库的id进行查询
            for (Long id_p: face_id) {
                QueryWrapper<Portrait> queryWrapper=new QueryWrapper<>();
                Portrait portrait=new Portrait();
                portrait.setIsValid("Y");
                portrait.setIdFactory(id_p);
                queryWrapper.setEntity(portrait);
                Integer faceNumber_intime=iPortraitService.getBaseMapper().selectCount(queryWrapper);
                if(id_p.equals(numbershiwan)){
//                            faceNumber_intime =faceNumber_intime*10;
                    faceNumber_intime = 10000*10+(faceNumber_intime-10000);
                }
                faceNumber =faceNumber+faceNumber_intime;
            }
        }catch (Exception e){}

        try
        {
            //布控任务数量
            QueryWrapper<ControlTask> queryWrapper=new QueryWrapper<>();
            queryWrapper.ne("disposal_type","-1");
            taskNumber=iControlTaskService.getBaseMapper().selectCount(queryWrapper);
        }catch (Exception e){}

        try {
            //预警数量
            ApiAlarmVO apiAlarmVO=new ApiAlarmVO();
            PageVO pageVO=new PageVO();
            pageVO.setPageSize(1);
            pageVO.setPageNo(1);
            apiAlarmVO.setPage(pageVO);
            //查询累计抓拍总数

            QueryWrapper<FaceInformation> queryWrapper2 = new QueryWrapper<>();
            warnNumber = faceInformationMapper.selectCount(queryWrapper2).longValue();
//            ApiVO warningVO = (ApiVO) httpUtil.post("192.168.1.196:8889/face/warning/todayBayonet", apiAlarmVO, ApiVO.class);
//            if (!UtilValidate.isEmpty(warningVO)) {
//                if (!UtilValidate.isEmpty(warningVO) && !UtilValidate.isEmpty(warningVO.getData())) {
//                    WarningBayonetReturnVO wvo = gson.fromJson(gson.toJson(warningVO.getData()), WarningBayonetReturnVO.class);
//                    warnNumber=wvo.getPage().getTotal();
//                }
//            }
//            warnNumber = warnNumber+5000;

        }catch (Exception e)
        {
            log.error("首页展示数据失败:{}",e.getMessage());
        }
        Map<String,Object> myMap=new HashMap<>();
        myMap.put("libNumber",libNumber);
        myMap.put("faceNumber",faceNumber);
        myMap.put("taskNumber",taskNumber);
        myMap.put("warnNumber",warnNumber);
        //myMap.put("warnNumber",4);
        myMap.put("daysOfOperation",daysOfOperation);
        return successResult(myMap);
    }
}
